package com.nq.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nq.common.ServerResponse;
import com.nq.pojo.User;
import com.nq.recharge.entity.StRechargeCard;
import com.nq.dao.StRechargeCardMapper;
import com.nq.recharge.resp.RechargeCardResp;
import com.nq.recharge.service.IStRechargeCardService;
import com.nq.service.IUserService;
import com.nq.utils.redis.RedisShardedPoolUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.util.List;

/**
 * @Description: 充值卡银行信息
 * @Author: jeecg-boot
 * @Date:   2024-11-12
 * @Version: V1.0
 */
@Service
public class StRechargeCardServiceImpl extends ServiceImpl<StRechargeCardMapper, StRechargeCard> implements IStRechargeCardService {

    @Resource
    private IUserService iUserService;

    @Value("${stocks.recharge_validity_period}")
    private String rechargeValidityPeriod;

    @Override
    public ServerResponse cardList(HttpServletRequest request) {

        User user = this.iUserService.getCurrentRefreshUser(request);
        if (null == user) {
            return ServerResponse.createByErrorMsg("User information does not exist !");
        }

        QueryWrapper<StRechargeCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<StRechargeCard> stRechargeCards = baseMapper.selectList(queryWrapper);
        return ServerResponse.createBySuccess(stRechargeCards);
    }

    @Override
    public ServerResponse queryCard(String password, HttpServletRequest request) {

        User user = this.iUserService.getCurrentRefreshUser(request);
        if (null == user) {
            return ServerResponse.createByErrorMsg("User information does not exist !");
        }


        QueryWrapper<StRechargeCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("password", password);
        StRechargeCard stRechargeCard = baseMapper.selectOne(queryWrapper);
        if (null == stRechargeCard) {
            return ServerResponse.createByErrorMsg("Can not query the corresponding recharge card information!");
        }

        RechargeCardResp cardResp = new RechargeCardResp();
        BeanUtils.copyProperties(stRechargeCard, cardResp);

        Long validity = Long.valueOf(rechargeValidityPeriod);
        // 以用户id 卡号  有效时间为key
        cardResp.setSeconds(validity);
//        String bankNo = stRechargeCard.getBankNo();
        // 当前时间戳（毫秒）
//        long millis = System.currentTimeMillis();
//        String key = user.getId() + ":" + bankNo;
//
//        String storageMillis = RedisShardedPoolUtils.get(key);
//        if (StringUtils.isBlank(storageMillis)) {
//            cardResp.setSeconds(validity);
//            RedisShardedPoolUtils.set(key, String.valueOf(millis));
//        } else {
//            // 获取redis中储存的时间戳（毫秒）
//            Long storedMillis = Long.valueOf(storageMillis);
//            // 将时间戳转换为Instant对象
//            Instant instant1 = Instant.ofEpochMilli(storedMillis);
//            Instant instant2 = Instant.ofEpochMilli(millis);
//
//            // 计算两个Instant对象之间的时间差
//            long secondsBetween = instant2.getEpochSecond() - instant1.getEpochSecond();
//            // 输出结果
//            cardResp.setSeconds(validity - secondsBetween);
//            if (secondsBetween > validity) {
//                return ServerResponse.createByErrorMsg("This password has expired, please contact the customer service staff!");
//            }
//
//        }
        return ServerResponse.createBySuccess(cardResp);
    }
}
